package com.edgclub.edg.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.edgclub.edg.R;
import com.edgclub.edg.adapter.adapteruitls.GzBaseAdapter;
import com.edgclub.edg.adapter.adapteruitls.MyItemClickListener;
import com.edgclub.edg.adapter.adapteruitls.MyItemLongClickListener;
import com.edgclub.edg.adapter.adapteruitls.RViewHolder;
import com.edgclub.edg.model.PostsAlbum;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.DensityUtils;

import java.util.List;

/**
 */
public class UserPhotoAdapter extends GzBaseAdapter {
    private List<PostsAlbum> datas = null;
    private Context context;
    private GlideUtils glideUtils;
    private int itemW;
    private boolean iseditor;
    private boolean ismy;
    public UserPhotoAdapter(Context context,boolean ismy) {
        this.context = context;
        this.ismy=ismy;
        itemW = (DensityUtils.getScreenW((Activity) context)-15) / 3;
        glideUtils = new GlideUtils();
    }

    //创建新View，被LayoutManager所调用
    @Override
    public UserPhotoAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_user_photo_grid,viewGroup,false);
        ViewHolder vh = new ViewHolder(view,mListener,mLongClickListener);
        return vh;
    }

    @SuppressWarnings("ResourceAsColor")
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final UserPhotoAdapter.ViewHolder viewHolder= (ViewHolder) holder;
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) viewHolder.ImageUser.getLayoutParams();
        lp.width = itemW;
        lp.height = itemW;
        viewHolder.ImageUser.setLayoutParams(lp);


            if(ismy&&position==0) {
                viewHolder.del.setVisibility(View.GONE);
                glideUtils.Glidde_loadImage(context,R.drawable.add_photo,viewHolder.ImageUser);
            }else {
                glideUtils.Glidde_loadImage(context,datas.get(position).getThumbnail(),viewHolder.ImageUser);
                viewHolder.del.setVisibility(View.VISIBLE);
            }
        if(iseditor){
            if(position!=0)
            viewHolder.del.setVisibility(View.VISIBLE);
        }else{

            viewHolder.del.setVisibility(View.GONE);
        }

        viewHolder.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(null!=mdelListener){
                    mdelListener.deletePhoto(position,datas.get(position).getAlbumId());
                }
            }
        });

    }


    //获取数据的数量
    @Override
    public int getItemCount() {
            return datas.size();
    }

    public List<PostsAlbum> getDatas() {
        return datas;
    }

    public void setDatas(List<PostsAlbum> datas) {
        this.datas = datas;
    }

    public void setEditor(boolean res){
        iseditor=res;
        notifyDataSetChanged();
    }


    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RViewHolder {
        private ImageView ImageUser,del;

        public ViewHolder(View view, MyItemClickListener listener, MyItemLongClickListener longClickListener) {
            super(view, listener, longClickListener);
            ImageUser = (ImageView) view.findViewById(R.id.imageView);
            del = (ImageView) view.findViewById(R.id.del);
        }

    }
    public interface delListener{
        public void deletePhoto(int position,String albumId);
    }
    private delListener mdelListener;

    public void setDelListener(delListener delListener) {
        this.mdelListener = delListener;
    }
}
